//
//  CollectionView2.swift
//  swiftStudy
//
//  Created by Zhang on 2021/6/16.
//

import SwiftUI

struct CollectionView2: View {
    
    let code_1: String
    let code_2: String
    let code_3: String
    let code_4: String
    let code_5: String
    let code_6: String
    
    init() {
        code_1 =
            "//数组定义\n" +
            "var array_1:[Int] = [1,2,3,4]\n" +
            "var array_2:Array<Int> = array_1\n" +
            "\n" +
            "//数组的创建可以使用两种方式,使用Array构造函数\n" +
            "//或者使用[]来创建\n" +
            "\n" +
            "//创建空数组\n" +
            "var array_3:[Int] = []\n" +
            "\n" +
            "//使用构造函数创建数组\n" +
            "var array_4 = Array<Int>(arrayLiteral:  1,2,3,4,5)"
        
        code_2 =
            "//直接遍历\n" +
            "for item in array_4{\n" +
            "    print(item)\n" +
            "}\n" +
            "\n" +
            "//角标遍历\n" +
            "for index in array_4.indices{\n" +
            "    print(index)\n" +
            "}\n" +
            "\n" +
            "//forEach遍历\n" +
            "array_4.forEach { item in\n" +
            "    print(item)\n" +
            "}"
        
        code_3 =
            "//按照从大到小排序\n" +
            "array_4.sorted(by:>)\n" +
            "//按照从小到大排序\n" +
            "array_4.sorted(by:<)\n" +
            "\n" +
            "//获取最大值和最小值\n" +
            "array_4.max()\n" +
            "array_4.min()"
        
        code_4 =
            "//创建Set集合,元素不能重复\n" +
            "var set_1:Set<Int> = [1,1,1,1]\n" +
            "//使用构造函数创建\n" +
            "var set_2 = Set(arrayLiteral: 1,2,3,4)\n" +
            "//打印结果为1\n" +
            "set_1.count"
        
        code_5 =
            """
            for item in set_2.sorted(by: >){
                print(item)
            }
            """
        
        code_6 =
            "//声明一个字典\n" +
            "var dic_1:[Int:String] = [1:\"one\",2:\"two\"]\n" +
            "//创建一个空字典\n" +
            "var dic_2:[Int:Int] = [:]\n" +
            "//对某个键进行赋值,如果存在更新值,不存在添加键值对\n" +
            "dic_2[1] = 1\n" +
            "\n" +
            "//遍历字典的key\n" +
            "for key in dic_1.keys{\n" +
            "    print(key)\n" +
            "}\n" +
            "//遍历value\n" +
            "for value in dic_1.values{\n" +
            "    print(value)\n" +
            "}\n" +
            "//遍历key和value\n" +
            "for (key,value) in dic_1{\n" +
            "    print(\"key:\\(key) value:\\(value)\")\n" +
            "}"
    }
    
    
    var body: some View {
        
        ScrollView(.vertical) {
            VStack(alignment: .leading, spacing: 5) {
                
                Section(header: SectionHeaderView(title: "集合类型")) {
                    CommonTextView(content: "Swift语言中提供了三种集合类型:Array(数组)、Set(集合)、Dictionary(字典)")
                    
                }
                
                Section(header: SectionHeaderView(title: "Array数组")) {
                    CommonTextView(content: "Array存放的所有数据类型必须统一,使用.append()方法添加元素")
                    
                    CodeView(code: code_1).padding(10)
                    
                    CommonTextView(content: "使用for-in和forEach来遍历数组")
                    
                    CodeView(code: code_2).padding(10)
                    
                    CommonTextView(content: "Array类型中还提供了一个排序函数")
                    
                    CodeView(code: code_3).padding(10)
                    
                }
                
                Section(header:SectionHeaderView(title: "Set集合")) {
                    CommonTextView(content: "Set集合不关注元素的顺序,但是其中的元素不可重复,使用.insert()方法添加元素")
                    
                    CodeView(code: code_4).padding(10)
                    
                    CommonTextView(content: "Set集合和Array数组除了有序和无序的区别外,还有一个独有的特点:Set集合可以进行数学运算,例如交集运算,并集运算,补集运算,使用==可以比较两个Set集合是否相等,当两个集合中的所有元素都相等时,两个集合才相等,集合虽然不强调顺序,但是开发的时候可以对其先进行排序再遍历,如下:")
                    
                    CodeView(code: code_5).padding(10)
                    
                }
                
                Section(header:SectionHeaderView(title: "Dictionary字典")) {
                    CommonTextView(content: "Dictionary是一种以key和value存储数据的集合,在字典中value可以重复,key必须唯一")
                    
                    CodeView(code: code_6).padding(10)
                    
                }
                
            }
            //设置VStack,横向填充屏幕
            .frame(minWidth: 0,
                   maxWidth: .infinity,
                   minHeight: 0,
                   maxHeight: .infinity,
                   alignment: .topLeading
            )
        }
    }
}

struct CollectionView2_Previews: PreviewProvider {
    static var previews: some View {
        CollectionView2()
    }
}
